package de.tabuma.lib.cm.crud;

import de.tabuma.lib.contentmanagement.*;
import de.tabuma.lib.contentmanagement.entity.Content;
import java.util.*;
import java.util.logging.*;
import javax.servlet.http.*;
import java.lang.reflect.*;
import de.tabuma.lib.*;

/**
 *
 * @author alefen
 */
public class ListViewFacade {

    private static final Logger log = Logger.getLogger(ListViewFacade.class.getName());

    public static String view(Content content, HttpServletRequest request) throws Exception {
        String str = "";
        String remove = "remove";

        List myList = (List) request.getAttribute(constants.OBJECT_LIST);
        
        Vector<String> tableHead = new Vector<String>();

        for(int i=0;i<content.ListContent.getListableFields().size();i++) {
            Field myField = content.ListContent.getListableFields().elementAt(i);
            String displayedField = content.FormContent.getDisplayedName().get(myField);
            tableHead.add(displayedField);
        }
        Enumeration<String> enu = content.ListContent.getAdditionalListFields().keys();
        while(enu.hasMoreElements()) {
            String key = enu.nextElement();
            tableHead.add(key);
        }
        tableHead.add(remove);

        String linkRemove = content.GeneralContent.getServlet() + "?" + view.getLinkAsAttribute(content.GeneralContent.getParameter())+"&amp;"+content.GeneralContent.getParamName()+"="+constants.REMOVE;
        String linkUpdate = content.GeneralContent.getServlet() + "?" + view.getLinkAsAttribute(content.GeneralContent.getParameter())+"&amp;"+content.GeneralContent.getParamName()+"="+constants.UPDATE;
        
        Vector<Vector<String>> elements = new Vector<Vector<String>>();
        
        for(int i=0;i<myList.size();i++) {
            Object objekt = myList.get(i);
            
            Vector<String> contentList = new Vector<String>();
            String fieldId = "id";
            String methodName = ObjectMethods.getGetter(fieldId);
            Long id = (Long) objekt.getClass().getMethod(methodName).invoke(objekt);
            for(int j=0;j<content.ListContent.getListableFields().size();j++) {
                Field field = content.ListContent.getListableFields().elementAt(j);
                String functionName = ObjectMethods.getGetter(field.getName());
                String fieldElement = "" + objekt.getClass().getMethod(functionName).invoke(objekt);
                if(content.ListContent.getListParameterDisplayedField().get(field)!=null) {
                    try {
                        Integer fieldKey = (Integer) objekt.getClass().getMethod(ObjectMethods.getGetter(field.getName())).invoke(objekt);
                        List<Field> myField = content.ListContent.getListParameterDisplayedField().get(field);
                        List contentField = content.ListContent.getListParameterContent().get(field);
                        for(int k=0;k<contentField.size();k++) {
                            Object obj = contentField.get(k);
                            Long idValue = (Long) obj.getClass().getMethod(ObjectMethods.getGetter("id")).invoke(obj);
                            if(idValue.intValue()==fieldKey.intValue()) {
                                fieldElement = "";
                                for(int l=0;l<myField.size();l++) {
                                    if(l!=0) {
                                        fieldElement += ", ";
                                    }
                                    fieldElement += "" + obj.getClass().getMethod(ObjectMethods.getGetter(myField.get(l).getName())).invoke(obj);
                                }
                                k = contentField.size();
                            }
                        }
                    }
                    catch(Exception e) {
                        String fieldKey = (String) objekt.getClass().getMethod(ObjectMethods.getGetter(field.getName())).invoke(objekt);
                        List<Field> myField = content.ListContent.getListParameterDisplayedField().get(field);
                        List contentField = content.ListContent.getListParameterContent().get(field);
                        for(int k=0;k<contentField.size();k++) {
                            Object obj = contentField.get(k);
                            String idValue = (String) obj.getClass().getMethod(ObjectMethods.getGetter("id")).invoke(obj);
                            if(idValue.equals(fieldKey)) {
                                fieldElement = "";
                                for(int l=0;l<myField.size();l++) {
                                    if(l!=0) {
                                        fieldElement += ", ";
                                    }
                                    fieldElement += "" + obj.getClass().getMethod(ObjectMethods.getGetter(myField.get(l).getName())).invoke(obj);
                                }
                                k = contentField.size();
                            }
                        }
                    }
                }
                contentList.addElement("<a href=\""+linkUpdate+"&amp;"+content.GeneralContent.getParamName()+constants.id+"="+id+"\">"+fieldElement+"</a>");
            }

            Enumeration<String> enu1 = content.ListContent.getAdditionalListFields().keys();

            while(enu1.hasMoreElements()) {
                String newElement = "";
                String key = enu1.nextElement();
                Hashtable<String,String> value = content.ListContent.getAdditionalListFields().get(key);
                newElement += content.GeneralContent.getParamName()+constants.id+"="+id+"&amp;"+view.getLinkAsAttribute(value)+"&amp;"+view.getLinkAsAttribute(content.GeneralContent.parameter);
                newElement = "<a href=\""+content.GeneralContent.getServlet()+"?"+newElement+"\">"+key+"</a>";
                contentList.addElement(newElement);
            }
            contentList.addElement("<a href=\""+linkRemove+"&amp;"+content.GeneralContent.getParamName()+constants.id+"="+id+"\">"+remove+"</a>");
            elements.add(contentList);
        }

        Hashtable addParameter = new Hashtable();
        addParameter.put(content.GeneralContent.getParamName(), ""+constants.CREATE);
        addParameter.putAll(content.GeneralContent.getParameter());

        if(request.getAttribute(constants.MESSAGE_ERROR)!=null) {
            str += "<div class=\"errormessage\"><div class=\"errormessage2\">"+request.getAttribute(constants.MESSAGE_ERROR)+"</div></div>\n";
        }
        if(request.getAttribute(constants.MESSAGE_INFO)!=null) {
            str += "<div class=\"infomessage\"><div class=\"infomessage2\">"+request.getAttribute(constants.MESSAGE_INFO)+"</div></div>\n";
        }

        str += view.getTable(content.GeneralContent.getServlet(), content.GeneralContent.getParameter(), tableHead, elements , addParameter, "submit");

        return str;
    }
}